<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="策略名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入策略名"
          clearable
        />
      </el-form-item>
      <el-form-item label="策略类型" prop="type">
        <el-select v-model="queryParams.type" filterable  clearable placeholder="请选择类型" style="width:255px">
          <el-option
            v-for="item in strategy_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="执行效率" prop="executoryEffect">
        <el-select v-model="queryParams.executoryEffect" filterable  clearable placeholder="请选择类型" style="width:255px">
          <el-option
            v-for="item in executory_effect"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:produce-plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:produce-plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['produce:produce-plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getSnapStrategyList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="snapStrategyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="策略名" align="center" prop="name" />
      <el-table-column label="策略类型" align="center" prop="type" >
        <template slot-scope="scope">
          {{  scope.row.type === 1 ? '周期上报控制':'时间段上报控制' }}
        </template>
      </el-table-column>
      <el-table-column label="策略时间" align="center" prop="time" >
        <template slot-scope="scope">
          <div v-if="scope.row.time && scope.row.time.length > 0">
            <div v-for="item in scope.row.time" :key="item">
              {{ parseInt(item.replace('[',''))  }}:00 ~ {{ parseInt(item.replace('[','')) + 1}}:00
            </div>
          </div>
          <div v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column label="最小抓图间隔时间(s)" align="center" prop="interval" />
      <el-table-column label="策略时间内最大抓图数" align="center" prop="numLimit" />
      <el-table-column label="执行效率" align="center" prop="executoryEffect">
        <template slot-scope="scope">
          {{  scope.row.executoryEffect === 0 ? '非强制条件':'强制条件' }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSnapStrategyList "
    />

    <!-- 添加或修改抓图策略对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="抓图策略名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入策略名称"  maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="策略类型" prop="type">
              <el-select v-model="form.type" filterable  placeholder="请选择类型" style="width:255px">
                <el-option
                  v-for="item in strategy_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行效率" prop="executoryEffect">
              <el-select v-model="form.executoryEffect" filterable  placeholder="请选择执行效率" style="width:255px">
                <el-option
                  v-for="item in executory_effect"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="策略时间(时间整点)" prop="times">
              <el-select v-model="form.times" multiple placeholder="请选择" @change="$forceUpdate()">
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最小抓图间隔" prop="interval">
              <el-input v-model="form.interval" placeholder="请输入最小抓图间隔" type="number" oninput="if(value.length > 4)value =value.slice(0,4)"> 
                <i slot="suffix"  style="font-style:normal;margin-right: 10px; line-height: 30px; color: #1e1e1e">秒</i>
              </el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="策略时间内最大抓图" prop="numLimit">
              <el-input v-model="form.numLimit" placeholder="请输入抓图数" type="number" oninput="if(value.length > 5)value =value.slice(0,5)"> 
                <i slot="suffix"  style="font-style:normal;margin-right: 10px; line-height: 30px; color: #1e1e1e">张</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"  maxlength="30" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>        

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import Select from '@/components/SelectChecked'
import { listSnapStrategy, getSnapStrategyDetail, addSnapStrategy, updateSnapStrategy, exportSnapStrategy, delSnapStrategy } from "@/api/device/snapStrategy";

export default {
  name: "snapStrategy",
  components: {
    Select
  },
  data() {
    return {
      // 表单数据
      form : {
        address: null,
        classify:null,
        code: null,
        direction: null,
        model: null,
        name: null,
        paimianAngle: null,
        port: null,
        type: 1,
        times:null,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中项目名数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抓图策略数据
      snapStrategyList: [],
      // 策略类型
      strategy_type: [
        {label:'周期上报控制',value:1},
        {label:'时间段上报控制',value:2}
      ],
      // 执行效率
      executory_effect: [
        {label:'非强制条件',value:0},
        {label:'强制条件',value:1}
      ],
      // 时间段
      options: [
        {value: 0,label: '0:00 ~ 0:59',check: false},
        {value: 1,label: '1:00 ~ 1:59', check: false},
        {value: 2,label: '2:00 ~ 2:59', check: false},
        {value: 3,label: '3:00 ~ 3:59', check: false},
        {value: 4,label: '4:00 ~ 4:59', check: false},
        {value: 5,label: '5:00 ~ 5:59', check: false},
        {value: 6,label: '6:00 ~ 6:59', check: false},
        {value: 7,label: '7:00 ~ 7:59', check: false},
        {value: 8,label: '8:00 ~ 8:59', check: false},
        {value: 9,label: '9:00 ~ 9:59', check: false},
        {value: 10,label: '10:00 ~ 10:59',check: false},
        {value: 11,label: '11:00 ~ 11:59',check: false},
        {value: 12,label: '12:00 ~ 12:59',check: false},
        {value: 13,label: '13:00 ~ 13:59',check: false},
        {value: 14,label: '14:00 ~ 14:59',check: false},
        {value: 15,label: '15:00 ~ 15:59',check: false},
        {value: 16,label: '16:00 ~ 16:59',check: false},
        {value: 17,label: '17:00 ~ 17:59',check: false},
        {value: 18,label: '18:00 ~ 18:59',check: false},
        {value: 19,label: '19:00 ~ 19:59',check: false},
        {value: 20,label: '20:00 ~ 20:59',check: false},
        {value: 21,label: '21:00 ~ 21:59',check: false},
        {value: 22,label: '22:00 ~ 22:59',check: false},
        {value: 23,label: '23:00 ~ 23:59',check: false}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        executoryEffect: null,
        name: null,
        remark: null,
        orderByColumn:'id',
        isAsc:'desc'
      },
      // 表单校验
      rules: {
        executoryEffect: [
          { required: true, message: "执行效率不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "策略类型不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "抓图策略名称不能为空", trigger: "blur" }
        ],
        times: [
          { required: true, message: "策略时间不能为空", trigger: "blur" }
        ],
        interval: [
          { required: true, message: "最小抓图间隔不能为空", trigger: "blur" }
        ],
        numLimit: [
          { required: true, message: "抓图数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getSnapStrategyList();
  },
  methods: {
    /** 选中抓图时间段 */
    selected(value){
      this.form.times = value
      console.log("time : " + this.form.times);
    },
    /** 获取抓图策略列表 */
    getSnapStrategyList(){
      this.loading = true;
      listSnapStrategy(this.queryParams).then((res) =>{
        if(res.code === 200){
          if(res.rows){
            for(var i=0;i<res.rows.length;i++){
              res.rows[i].time = res.rows[i].time.split(',')
            }
            this.snapStrategyList = res.rows
            this.total = res.total
          }

        }
      }).catch((err) =>{
        this.$message.error("抓图策略列表获取失败，原因："+err.message)
      }).finally(() =>{
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name:'',
        type:'',
        times:'',
        executoryEffect:'',
        interval:'',
        numLimit:'',
        remark:''
      }
      this.options = [
        {value: 0,label: '0:00 ~ 0:59',check: false},
        {value: 1,label: '1:00 ~ 1:59', check: false},
        {value: 2,label: '2:00 ~ 2:59', check: false},
        {value: 3,label: '3:00 ~ 3:59', check: false},
        {value: 4,label: '4:00 ~ 4:59', check: false},
        {value: 5,label: '5:00 ~ 5:59', check: false},
        {value: 6,label: '6:00 ~ 6:59', check: false},
        {value: 7,label: '7:00 ~ 7:59', check: false},
        {value: 8,label: '8:00 ~ 8:59', check: false},
        {value: 9,label: '9:00 ~ 9:59', check: false},
        {value: 10,label: '10:00 ~ 10:59',check: false},
        {value: 11,label: '11:00 ~ 11:59',check: false},
        {value: 12,label: '12:00 ~ 12:59',check: false},
        {value: 13,label: '13:00 ~ 13:59',check: false},
        {value: 14,label: '14:00 ~ 14:59',check: false},
        {value: 15,label: '15:00 ~ 15:59',check: false},
        {value: 16,label: '16:00 ~ 16:59',check: false},
        {value: 17,label: '17:00 ~ 17:59',check: false},
        {value: 18,label: '18:00 ~ 18:59',check: false},
        {value: 19,label: '19:00 ~ 19:59',check: false},
        {value: 20,label: '20:00 ~ 20:59',check: false},
        {value: 21,label: '21:00 ~ 21:59',check: false},
        {value: 22,label: '22:00 ~ 22:59',check: false},
        {value: 23,label: '23:00 ~ 23:59',check: false}
      ],
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getSnapStrategyList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.times = ''
      console.log('this.form',this.form);
      
      this.open = true;
      this.title = "添加抓图策略";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.times = null
      const id = row.id || this.ids
      getSnapStrategyDetail(id).then(response => {
        response.data.time = response.data.time.slice(1,-1)
        var strArr = response.data.time.split(',')
        
        var intArr = []
        intArr = strArr.map(item =>{
          return +parseInt(item)
        })
        // console.log(intArr)
        // this.form.times = intArr
        // // response.data.times = intArr
        // const times = intArr
        // for(var i=0;i<times.length;i++){
          
        //   // this.selected(response.data.times[i])
        //     console.log(times[i]);
        //       this.options[times[i]].check = true
        // }
        // this.$emit('selected', this.options)
        // console.log(times)
        this.form = response.data;
        this.form.times = intArr;
        console.log('this.form',this.form);
        
        this.open = true;
        this.title = "修改抓图策略";
      });
      
    },
    /** 提交按钮 */
    submitForm(){
      console.log('dsfasdf',this.form.times);
      
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.times.sort()
            this.form.time = '[' + this.form.times.join(',') + ']'
            console.log("this.form",this.form)
            updateSnapStrategy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getSnapStrategyList();
            });
          } else {
            this.form.times.sort()
            this.form.time = '[' + this.form.times.join(',') + ']'
            console.log("this.form",this.form)
            addSnapStrategy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getSnapStrategyList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //todos 修改
      const ids = row.id || this.ids;
      const names = row.name || this.names
      this.$modal.confirm('是否确认抓图策略名为"' + names + '"的数据项？').then(function() {
          // 修改为逻辑删除
        return delSnapStrategy(ids);
      }).then(() => {
        this.getSnapStrategyList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/genius/platform-snap-strategy/export', {
        ...this.queryParams
      }, `抓图策略列表_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.formTitle{
  width:100%;
  text-align:center;
  margin:15px 0 15px 0;
  font-weight:border;
  font-size:20px
}
</style>
